﻿@page "/sdk/objects/entity/r/{RecordId}/rl/fields/c/{PageName?}"

@model WebVella.Erp.Plugins.SDK.Pages.ErpEntity.CreateFieldModel
@using WebVella.Erp.Web.Utils;
@{
	Layout = "~/Pages/_AppMaster.cshtml";
	ViewData["Title"] = "Create Field";
}


<wv-page-header color="@Model.ErpEntity.Color" area-label="Entities" title="@Model.ErpEntity.Name" subtitle="Create Field"
				icon-class="fa fa-plus" return-url="@Model.ReturnUrl">
	<wv-page-header-actions>
		@foreach (var action in Model.HeaderActions)
		{
			@Html.Raw(action)
		}
	</wv-page-header-actions>

	<wv-page-header-toolbar>
		@foreach (var action in Model.HeaderToolbar)
		{
			@Html.Raw(action)
		}
	</wv-page-header-toolbar>
</wv-page-header>

@if( Model.Type == FieldType.GeographyField && !Model.IsPostgisInstalled)
{
<div class="alert alert-danger">
    A field from this type cannot be created because it requires PostGIS extension installed in database. For PG 13 and later you can use stack build to install it, for older versions check 
    <a href="https://postgis.net/install/" target="_blank">postgis.net</a> .
</div>
}


<wv-row class="mt-4">
	<wv-column span="12">
		<wv-icon-card title="@Model.FieldCard["label"]" class="mb-4" description="@Model.FieldCard["description"]" icon-class="@Model.FieldCard["class"]"></wv-icon-card>
	</wv-column>
</wv-row>

<wv-validation errors="@Model.Validation.ToErrorList()" message="@Model.Validation.Message"></wv-validation>

<wv-form label-mode="Stacked" mode="Form" name="CreateRecord" validation-errors="Model.Validation.ToErrorList()" id="CreateRecord">
	<wv-section title="General">
		<wv-row>
			<wv-column span="6">
				<wv-field-text name="Name" value="Model.Name" label-text="Name" required="true" placeholder="e.g. field_name"></wv-field-text>
			</wv-column>
			<wv-column span="6">
				<wv-field-text name="Label" value="Model.Label" label-text="Label" required="true" placeholder="e.g. Field Name"></wv-field-text>
			</wv-column>
		</wv-row>
		<wv-row>
			<wv-column span="6">
				<wv-field-guid label-text="Id (Guid)" value="@Model.Id" name="Id" placeholder="leave empty to autogenerate"></wv-field-guid>
			</wv-column>
			<wv-column span="6">
				<wv-field-checkbox name="Required" value="Model.Required" label-text="Required" text-true="required field" text-false="not required field"></wv-field-checkbox>
			</wv-column>
		</wv-row>
		<wv-row>
			<wv-column span="6">
				<wv-field-text name="Description" value="Model.Description" label-text="Description"></wv-field-text>
			</wv-column>
			<wv-column span="6">
				<wv-field-checkbox name="Unique" value="Model.Unique" label-text="Unique" text-true="all values should be unique" text-false="values not unique"></wv-field-checkbox>
			</wv-column>
		</wv-row>
		<wv-row>
			<wv-column span="6">
				<wv-field-text name="HelpText" value="Model.HelpText" label-text="HelpText"></wv-field-text>
			</wv-column>
			<wv-column span="6">
				<wv-field-checkbox name="System" value="Model.System" label-text="System" text-true="system field" text-false="not a system field"></wv-field-checkbox>
			</wv-column>
		</wv-row>
		<wv-row>
			<wv-column span="6">
				<wv-field-text name="PlaceholderText" value="Model.PlaceholderText" label-text="PlaceholderText"></wv-field-text>
			</wv-column>
			<wv-column span="6">
				<wv-field-checkbox name="Searchable" value="Model.Searchable" label-text="Searchable" text-true="searchable" text-false="not searchable"></wv-field-checkbox>
			</wv-column>
		</wv-row>
	</wv-section>

    <wv-section title="Type specific" class="mt-3">
        @switch (Model.Type)
        {
            case FieldType.AutoNumberField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-number name="DefaultValue" value="Model.DefaultValue" label-text="Default value" required="true" placeholder="e.g. 1"></wv-field-number>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-number name="StartingNumber" value="Model.StartingNumber" label-text="Starting number" placeholder="increment starting point"></wv-field-number>
                    </wv-column>
                </wv-row>
                <wv-row>
                    <wv-column span="6">
                        <wv-field-text name="DisplayFormat" value="Model.DisplayFormat" label-text="Display Format" placeholder="e.g. Order-{0}"></wv-field-text>
                    </wv-column>
                    <wv-column span="6">
                    </wv-column>
                </wv-row>
                break;
            case FieldType.CheckboxField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-checkbox name="DefaultValue" value="Model.DefaultValue" label-text="Default value" required="true" text-true="checked by default" text-false="unchecked by default"></wv-field-checkbox>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.CurrencyField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-number name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" description="@("use 'null' to make nullable")"></wv-field-number>
                    </wv-column>
                </wv-row>
                <wv-row>
                    <wv-column span="6">
                        <wv-field-number name="MinValue" value="Model.MinValue" label-text="Minimum Value"></wv-field-number>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-number name="MaxValue" value="Model.MaxValue" label-text="Maximum Value"></wv-field-number>
                    </wv-column>
                </wv-row>
                <wv-row>
                    <wv-column span="6">
                        <wv-field-select options="Model.CurrencyOptions.ToWvSelectOption()" name="CurrencyCode" value="Model.CurrencyCode" label-text="Currency" required="true"></wv-field-select>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.DateField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-date name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" placeholder="e.g. 2014-11-01"></wv-field-date>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-text name="Format" value="Model.Format" label-text="Presentation format" placeholder="e.g. yyyy-MMM-dd"></wv-field-text>
                    </wv-column>
                </wv-row>
                <wv-row>
                    <wv-column span="6">
                        <wv-field-checkbox name="UseCurrentTimeAsDefaultValue" value="Model.UseCurrentTimeAsDefaultValue" label-text="Use generation date as default" text-true="use record date as default" text-false="do not use record date as default"></wv-field-checkbox>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.DateTimeField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-datetime name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" placeholder="e.g. 2014-11-01 23:01"></wv-field-datetime>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-text name="Format" value="Model.Format" label-text="Presentation format" placeholder="e.g. yyyy-MMM-dd HH:mm"></wv-field-text>
                    </wv-column>
                </wv-row>
                <wv-row>
                    <wv-column span="6">
                        <wv-field-checkbox name="UseCurrentTimeAsDefaultValue" value="Model.UseCurrentTimeAsDefaultValue" label-text="Use generation date as default" text-true="use record date as default" text-false="do not use record date as default"></wv-field-checkbox>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.EmailField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-text name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" placeholder="e.g. email@domain.com" description="@("use 'null' to make nullable")"></wv-field-text>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-number name="MaxLength" value="Model.MaxLength" label-text="Maximum Length"></wv-field-number>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.FileField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-text name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" placeholder="e.g. http://mydomain.com/file.pdf or /assets/file.pdf" description="@("use 'null' to make nullable")"></wv-field-text>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.GeographyField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-select options="Model.GeographyFormats.ToWvSelectOption()" name="GeographyFormat" value="Model.GeographyFormat" label-text="Geography format" required="true"></wv-field-select>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-number name="SRID" value="Model.SRID" label-text="SRID"></wv-field-number>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.HtmlField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-textarea name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" placeholder="e.g. some html code" description="@("use 'null' to make nullable")"></wv-field-textarea>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.ImageField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-text name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" placeholder="e.g. /assets/image.png" description="@("use 'null' to make nullable")"></wv-field-text>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.MultiLineTextField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-textarea name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" placeholder="e.g. some text" description="@("use 'null' to make nullable")"></wv-field-textarea>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.MultiSelectField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-textarea height="100px" name="SelectOptions" value="Model.SelectOptions" label-text="Options" description="(value) or (value,label) or (value,label,icon class) or (value,label,icon class,color) separated by new line"></wv-field-textarea>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-textarea height="100px" name="DefaultValue" value="Model.DefaultValue" label-text="Default Values" description="values separated by new line"></wv-field-textarea>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.NumberField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-number name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" description="@("use 'null' to make nullable")"></wv-field-number>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-number name="DecimalPlaces" value="Model.DecimalPlaces" label-text="Decimal Places"></wv-field-number>
                    </wv-column>
                </wv-row>
                <wv-row>
                    <wv-column span="6">
                        <wv-field-number name="MinValue" value="Model.MinValue" label-text="Minimum Value"></wv-field-number>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-number name="MaxValue" value="Model.MaxValue" label-text="Maximum Value"></wv-field-number>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.PasswordField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-number name="MaxLength" value="Model.MaxLength" label-text="Maximum Length"></wv-field-number>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-checkbox name="Encrypted" value="Model.Encrypted" label-text="Encryption" text-true="encrypted in database" text-false="plain text in database"></wv-field-checkbox>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.PercentField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-number name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" description="@("use 'null' to make nullable")"></wv-field-number>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-number name="DecimalPlaces" value="Model.DecimalPlaces" label-text="Decimal Places"></wv-field-number>
                    </wv-column>
                </wv-row>
                <wv-row>
                    <wv-column span="6">
                        <wv-field-number name="MinValue" value="Model.MinValue" label-text="Minimum Value"></wv-field-number>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-number name="MaxValue" value="Model.MaxValue" label-text="Maximum Value"></wv-field-number>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.PhoneField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-text name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" description="@("use 'null' to make nullable")"></wv-field-text>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-number name="MaxLength" value="Model.MaxLength" label-text="Maximum Length"></wv-field-number>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.GuidField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-text name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" description="@Html.Raw("fill with <a class='WvGuidPrefillEmpty' href='#'>empty guid</a>")"></wv-field-text>
                        <script>
                            $(function () {
                                $('.WvGuidPrefillEmpty').click(
                                    function (event) {
                                        event.preventDefault();
                                        event.stopPropagation()
                                        $(this).closest('.form-group').find('.form-control').val('00000000-0000-0000-0000-000000000000');
                                    }
                                );
                            });
                        </script>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-checkbox name="GenerateNewId" value="Model.GenerateNewId" label-text="Guid Generation" text-true="generate GUID as default" text-false="use default value"></wv-field-checkbox>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.SelectField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-textarea height="100px" name="SelectOptions" value="Model.SelectOptions" label-text="Options" description="(value) or (value,label) or (value,label,icon class) or (value,label,icon class,color) separated by new line"></wv-field-textarea>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-text name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" placeholder="" description="@("use 'null' to make nullable")"></wv-field-text>
                    </wv-column>
                </wv-row>

                break;
            case FieldType.TextField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-text name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" placeholder="" description="@("use 'null' to make nullable")"></wv-field-text>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-number name="MaxLength" value="Model.MaxLength" label-text="Maximum Length"></wv-field-number>
                    </wv-column>
                </wv-row>
                break;
            case FieldType.UrlField:
                <wv-row>
                    <wv-column span="6">
                        <wv-field-text name="DefaultValue" value="Model.DefaultValue" label-text="Default Value" placeholder="" description="@("use 'null' to make nullable")"></wv-field-text>
                    </wv-column>
                    <wv-column span="6">
                        <wv-field-number name="MaxLength" value="Model.MaxLength" label-text="Maximum Length"></wv-field-number>
                    </wv-column>
                </wv-row>
                <wv-row>
                    <wv-column span="6">
                        <wv-field-checkbox name="OpenTargetInNewWindow" value="Model.OpenTargetInNewWindow" label-text="Link target" text-true="open in new tab" text-false="open in same tab"></wv-field-checkbox>
                    </wv-column>
                </wv-row>
                break;
            default:
                break;
        }
    </wv-section>

	<wv-section title="API Security" class="mt-3">
		<wv-row>
			<wv-column span="6">
				<wv-field-checkbox name="EnableSecurity" value="Model.EnableSecurity" label-text="Field Security" text-true="field security enabled" text-false="no field security"></wv-field-checkbox>
			</wv-column>
		</wv-row>
		<wv-row>
			<wv-column span="12">
				<wv-field-checkbox-grid name="FieldPermissions" label-text="Field Permissions" columns="Model.PermissionOptions.ToWvSelectOption()" rows="Model.RoleOptions.ToWvSelectOption()" value="@Model.FieldPermissions" text-true="granted" text-false="not granted"></wv-field-checkbox-grid>
			</wv-column>
		</wv-row>
	</wv-section>
</wv-form>